﻿using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PhoenixCrabV6.Engine.Images
{
    [DataContract]
    public class MultiImage : Image, IMultiImage
    {
        /// <summary>
        /// The paths of the source images within this MultiImage
        /// </summary>
        [DataMember]
        public List<string> PathList { get; set; }

        /// <summary>
        /// The rendered display image
        /// </summary>
        [XmlIgnore] public override Texture2D DisplayImage { get { return this[DrawAtIndex]; } }

        /// <summary>
        /// Retrieve a specific image in the MultiImage
        /// </summary>
        /// <param name="i">index of the image to retrieve</param>
        /// <returns>Image as a Texture2D</returns>
        [XmlIgnore] public Texture2D this[int i]
        {
            get { return DisplayImageList[i]; }
        }

        /*[XmlElement("Position")]
        [DataMember(Name = "Position")]
        public Vector2 PositionDisplace { get; set; }*/

        /// <summary>
        /// When Draw method of the MultiImage called, this is the index of the image that gets displayed
        /// </summary>
        [DataMember]
        public int DrawAtIndex { get; set; }

        [XmlIgnore] public override string Path
        {
            get { return PathList[0]; }
        }

        [XmlIgnore] protected List<Texture2D> DisplayImageList;

        public MultiImage()
        {
            DrawAtIndex = 0;
        }

        public override void LoadContent()
        {
            base.LoadContent();

            //todo check this
            if (DisplayImageList == null)
                DisplayImageList = new List<Texture2D>();
            
            PathList.ForEach(p => DisplayImageList.Add(content.Load<Texture2D>(p)));
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            origin = new Vector2(CropRange.Width / 2, CropRange.Height / 2);
            spriteBatch.Draw(DisplayImage, (Position + origin), CropRange, Color.White * Alpha, 0.0f, origin, Scale, SpriteEffects.None,
                0.0f);
        }
    }
}